package com.yao2san.simdif.service;

import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.yao2san.simdif.enums.DiffType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.LinkedHashSet;
import java.util.List;

@Data
public class Result {
    /**
     * 差异汇总描述
     */
    private String description;
    /**
     * 差异汇总
     */
    private DatasourceSummary summary;

    private DatasourceDiff datasourceDiff;
    private List<TableDiff> tableDiffs;


    /**
     * 数据源差异汇总
     */
    @Data
    public static class DatasourceSummary {
        /**
         * 表数量
         */
        private Integer leftTableCount = 0;
        /**
         * 表数量
         */
        private Integer rightTableCount = 0;
        /**
         * 差异总数量(含不同的和缺失的)
         */
        private Integer diffCount = 0;
        /**
         * 缺失的表数量
         */
        private Integer lackTableCount = 0;
        /**
         * 有差异的表数量
         */
        private Integer diffTableCount = 0;
        /**
         * 字段差异数量(含字段属性差异和属性缺失的)
         */
        private Integer leftColumnDiffCount = 0;
        /**
         * 字段缺失数量
         */
        private Integer leftColumnLackCount = 0;
        /**
         * 字段差异数量(含字段属性差异和属性缺失的)
         */
        private Integer rightColumnDiffCount = 0;
        /**
         * 字段缺失数量
         */
        private Integer rightColumnLackCount = 0;
    }

    @Data
    public static class DatasourceDiff {
        private DatasourceAttr left;
        private DatasourceAttr right;
    }

    @Data
    public static class DatasourceAttr {
        private String name;
        private String database;
        private Integer tableCount;
        private Integer produceCount;
        private Integer functionCount;
        private LinkedHashSet<DiffInstance> diff;
    }

    /**
     * 表差异
     */
    @Data
    public static class TableDiff {
        private TableAttr left;
        private TableAttr right;
        private DiffType diffType;
        private Integer diffCount = 0;
    }

    /**
     * 表差异
     */
    @Data
    public static class TableAttr {
        private String tableName;
        private String tableComment;
        private String tableType;
        private String tableEngine;
        private String tableCollation;
        private String tableRows;
        private String tableIndex;
        private LocalDateTime tableCreateTime;
        private LocalDateTime tableUpdateTime;
        private LinkedHashSet<DiffInstance> diff;
        private List<ColumnAttr> columnAttrs;
        private List<ColumnDiff> columnDiffs;
        private DiffType diffType;

    }


    /**
     * 表差异
     */
    @Data
    public static class ColumnDiff {
        ColumnAttr left;
        ColumnAttr right;
        private DiffType diffType;
        private Integer diffCount = 0;
    }

    @Data
    public static class ColumnAttr {
        private String columnName;
        private String columnComment;
        private String columnType;
        private String columnDataType;
        private Integer columnLength;
        private Object columnDefault;
        private String columnIndex;
        private Boolean columnNull;
        private Integer columnSort;
        private LinkedHashSet<DiffInstance> diff;
        private DiffType diffType;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class DiffInstance {
        private String key;
        private Object leftValue;
        private Object rightValue;
        private DiffType diffType;
    }

    public static void main(String[] args) {
        TableAttr tableAttrs = new TableAttr();
        System.out.println(tableAttrs);
        System.out.println(JSONUtil.toJsonStr(new Result(), JSONConfig.create().setIgnoreNullValue(false)));
    }
}
